package cn.iocoder.yudao.module.system.controller.admin.chat;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.chat.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.chat.ChatDO;
import cn.iocoder.yudao.module.system.service.chat.ChatService;

@Tag(name = "管理后台 - 客服")
@RestController
@RequestMapping("/system/chat")
@Validated
public class ChatController {

    @Resource
    private ChatService chatService;

    @PostMapping("/create")
    @Operation(summary = "创建客服")
    @PreAuthorize("@ss.hasPermission('system:chat:create')")
    public CommonResult<Long> createChat(@Valid @RequestBody ChatSaveReqVO createReqVO) {
        return success(chatService.createChat(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新客服")
    @PreAuthorize("@ss.hasPermission('system:chat:update')")
    public CommonResult<Boolean> updateChat(@Valid @RequestBody ChatSaveReqVO updateReqVO) {
        chatService.updateChat(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除客服")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:chat:delete')")
    public CommonResult<Boolean> deleteChat(@RequestParam("id") Long id) {
        chatService.deleteChat(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客服")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:chat:query')")
    public CommonResult<ChatRespVO> getChat(@RequestParam("id") Long id) {
        ChatDO chat = chatService.getChat(id);
        return success(BeanUtils.toBean(chat, ChatRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得客服分页")
    @PreAuthorize("@ss.hasPermission('system:chat:query')")
    public CommonResult<PageResult<ChatRespVO>> getChatPage(@Valid ChatPageReqVO pageReqVO) {
        PageResult<ChatDO> pageResult = chatService.getChatPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ChatRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出客服 Excel")
    @PreAuthorize("@ss.hasPermission('system:chat:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportChatExcel(@Valid ChatPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ChatDO> list = chatService.getChatPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "客服.xls", "数据", ChatRespVO.class,
                        BeanUtils.toBean(list, ChatRespVO.class));
    }

}